<template>
  <el-dialog
    :title="!dataForm.categoryId ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="分类名称" prop="categoryName">
      <el-input v-model="dataForm.categoryName" placeholder="分类名称"></el-input>
    </el-form-item>
    <el-form-item label="分类编码" prop="categoryCode">
      <el-input v-model="dataForm.categoryCode" placeholder="分类编码"></el-input>
    </el-form-item>
    <el-form-item label="父分类ID" prop="parentId">
      <el-input v-model="dataForm.parentId" placeholder="父分类ID"></el-input>
    </el-form-item>
    <el-form-item label="分类层级" prop="categoryLevel">
      <el-input v-model="dataForm.categoryLevel" placeholder="分类层级"></el-input>
    </el-form-item>
    <el-form-item label="分类状态" prop="categoryStatus">
      <el-input v-model="dataForm.categoryStatus" placeholder="分类状态"></el-input>
    </el-form-item>
    <el-form-item label="最后修改时间" prop="modifiedTime">
      <el-input v-model="dataForm.modifiedTime" placeholder="最后修改时间"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          categoryId: 0,
          categoryName: '',
          categoryCode: '',
          parentId: '',
          categoryLevel: '',
          categoryStatus: '',
          modifiedTime: '',
          deleted: ''
        },
        dataRule: {
          categoryName: [
            { required: true, message: '分类名称不能为空', trigger: 'blur' }
          ],
          categoryCode: [
            { required: true, message: '分类编码不能为空', trigger: 'blur' }
          ],
          parentId: [
            { required: true, message: '父分类ID不能为空', trigger: 'blur' }
          ],
          categoryLevel: [
            { required: true, message: '分类层级不能为空', trigger: 'blur' }
          ],
          categoryStatus: [
            { required: true, message: '分类状态不能为空', trigger: 'blur' }
          ],
          modifiedTime: [
            { required: true, message: '最后修改时间不能为空', trigger: 'blur' }
          ],
          deleted: [
            { required: true, message: '逻辑删除（0.删除 1.未删除）不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.categoryId = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.categoryId) {
            this.$http({
              url: this.$http.adornUrl(`/childrenwear_product/product/productcategory/info/${this.dataForm.categoryId}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.categoryName = data.productCategory.categoryName
                this.dataForm.categoryCode = data.productCategory.categoryCode
                this.dataForm.parentId = data.productCategory.parentId
                this.dataForm.categoryLevel = data.productCategory.categoryLevel
                this.dataForm.categoryStatus = data.productCategory.categoryStatus
                this.dataForm.modifiedTime = data.productCategory.modifiedTime
                this.dataForm.deleted = data.productCategory.deleted
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/childrenwear_product/product/productcategory/${!this.dataForm.categoryId ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'categoryId': this.dataForm.categoryId || undefined,
                'categoryName': this.dataForm.categoryName,
                'categoryCode': this.dataForm.categoryCode,
                'parentId': this.dataForm.parentId,
                'categoryLevel': this.dataForm.categoryLevel,
                'categoryStatus': this.dataForm.categoryStatus,
                'modifiedTime': this.dataForm.modifiedTime,
                'deleted': this.dataForm.deleted
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
